#include "AnaDef.h"
#include "dEdxGausN.h"
#include "style.h"

const Double_t lgmar = 0.4;
const TString types[]={"p","#pi","K","e"};

void drawEtaCorrection(const Int_t kopt)
{
  if(kopt!=0 && kopt!=1){
    printf("bad kopt! %d\n", kopt); exit(1);
  }

  fgkYTitleOffset = 1.3;//default 1.1

  SetGlobalStyle(0);


  TCanvas *c1=new TCanvas;
 
  PadSetup(c1);
  c1->SetLeftMargin(0.14);
  c1->SetRightMargin(0.03);
  c1->SetTopMargin(0.07);

  gPad->SetLogx();

  TLegend *lg =new TLegend(0.7, 0.3, 0.9, 0.7);
  ResetStyle(lg);//, lgmar);

  const Int_t col[]={kBlue, kRed, kGreen+3, kMagenta};
  const Int_t lsty[]={kDashed, kSolid, kDotted};
  const Int_t msty[]={32, 26, 4};
  TH1D *h0[16];
  const Int_t ietas[]={0,3};
  const Double_t etas0[]={0.0, 0.6};
  const Double_t etas1[]={0.2, 0.8};
  Int_t nprint = 0;
  for(Int_t geta=0; geta<2; geta++){
    //skip eta0 for dYratio
    if(geta!=1 && kopt ==1) continue;

    const Double_t eta0 = etas0[geta];
    const Double_t eta1 = etas1[geta];

    for(Int_t itype=0; itype<4; itype++){
      //skip pion for etaratio
      if(itype==1 && kopt == 0) continue;

      h0[geta+4*itype]=new TH1D(Form("hEta%dPar%d",ietas[geta], itype),Form("%s %0.1f<|#eta|<%0.1f", types[itype].Data(), eta0, eta1),20,0.5,10);
      TH1D *hh=h0[geta+4*itype];
      BinLogX(hh->GetXaxis());
      hh->SetLineColor(col[itype]);      
      hh->SetLineStyle(lsty[geta]);
      hh->SetLineWidth(2);
      hh->SetMarkerStyle(msty[geta]);
      hh->SetMarkerSize(1);
      hh->SetMarkerColor(col[itype]);

      for(Int_t ix=1; ix<=hh->GetNbinsX(); ix++){
        const Double_t ptmean = hh->GetBinCenter(ix);

        Double_t iy = -999;
        if(kopt==0){
          //dEdxGausN::RatioEtaCorrection: dpion / dtype
          //eetacor: dtype / d-pion
          const Double_t etacor =  1./dEdxGausN::RatioEtaCorrection(itype, ptmean, eta0, eta1);
          iy = etacor;
        }
        else if(kopt==1){
          const Double_t dyratio = dEdxGausN::dYRatio(itype, ptmean, ietas[geta]);
          iy = dyratio-1;
        }
        printf("pls check %d %e %e %e -- %e\n", itype, ptmean, eta0, eta1, iy);

        hh->SetBinContent(ix, iy);
        hh->SetBinError(ix,0);
      }

      hh->SetXTitle("p_{T} (GeV/c)");
      if(kopt==0){
        hh->SetYTitle("#Deltay/#Deltay_{#pi}");
        hh->SetMinimum(0.45);
        hh->SetMaximum(1.1);
      }
      else if(kopt==1){
        hh->SetYTitle("#Deltay|_{0.6<|#eta|<0.8}/#Deltay|_{|#eta|<0.2}-1");
        hh->SetMinimum(-5e-3);
        hh->SetMaximum(0.2);
      }
      //hh->Draw(nprint?"same pl":"pl");
      hh->Draw(nprint?"same l":"l");
      nprint++;

      //lg->AddEntry(hh,hh->GetTitle(), "pl");
      lg->AddEntry(hh, kopt==0? hh->GetTitle() : types[itype].Data(), "l");
    }
  }

  lg->Draw();
  if(kopt==0)
    c1->Print("etaCorr.eps");
  else if(kopt==1)
    c1->Print("dyRatio.eps");
}

int main(int argc, char* argv[])
{
  for(Int_t ii=0; ii<argc; ii++){
    printf("%d %s\n", ii, argv[ii]);
  }
  if(argc!=2){
    printf("bad argc!\n"); return 1;
  }

  drawEtaCorrection(atoi(argv[1]));
  return 0;
}
